package pers.cz.controller;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.cz.model.AutoUiTestCase;
import pers.cz.mxgraph.GraphParser;
import pers.cz.mxgraph.MxCell;
import pers.cz.mxgraph.MxGraphModel;
import pers.cz.pub.RespBean;
import pers.cz.service.AutoUiTestCaseService;
import pers.cz.session.UserUtils;
import pers.cz.thread.AutoUiExcutor;
import pers.cz.thread.AutoUiThreadPool;

import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: AutoUiController
 * @author: Cheng Zhi
 * @create: 2023-01-18 16:40
 **/
@RestController
@RequestMapping(path = "/api/autoUi")
public class AutoUiController {

    @Autowired
    AutoUiTestCaseService autoUiTestCaseService;

    @PostMapping("startRun")
    public RespBean startRun(@RequestBody Map map) throws DocumentException {

        String xml = (String) map.get("mxGraphXml");
        String flowId = (String) map.get("flowId"); // flowId如果是立即运行的话，flowId就应该是一个随机生成的，不应该是固定的
        GraphParser graphParser = new GraphParser(xml);
        MxGraphModel mxGraphModel = graphParser.parse();
        MxCell startCell = mxGraphModel.getStartCell();

        // 异步执行
        AutoUiThreadPool.getThreadPool(flowId).execute(new Runnable() {
            @Override
            public void run() {
                AutoUiExcutor autoUiExcutor = new AutoUiExcutor();
                autoUiExcutor.run(startCell, flowId);
            }
        });

        return RespBean.ok("发起成功");
    }

    @PostMapping("saveAutoUiTestCase")
    public RespBean saveAutoUiTestCase(@RequestBody Map map) {

        String xml = (String) map.get("mxGraphXml");
        String flowName = (String) map.get("flowName");
        AutoUiTestCase autoUiTestCase = new AutoUiTestCase();
        autoUiTestCase.setFlowCfg(xml);
        autoUiTestCase.setFlowName(flowName);
        Integer save = autoUiTestCaseService.save(autoUiTestCase);
        if (save > 0) {
            return RespBean.ok("保存成功");
        } else {
            return RespBean.ok("保存失败");
        }

    }

    @PostMapping("deleteAutoUiTestCase")
    public RespBean deleteAutoUiTestCase(@RequestBody Map map) {
        String flowId = (String) map.get("flowId");

        AutoUiTestCase autoUiTestCase = new AutoUiTestCase();
        autoUiTestCase.setFlowId(Long.valueOf(flowId));
        Integer modify = autoUiTestCaseService.delete(autoUiTestCase);
        return RespBean.ok("成功删除" + modify + "条");
    }

    @PostMapping("updateAutoUiTestCase")
    public RespBean updateAutoUiTestCase(@RequestBody Map map) {

        String xml = (String) map.get("mxGraphXml");
        String flowId = (String) map.get("flowId");
        AutoUiTestCase autoUiTestCase = new AutoUiTestCase();
        autoUiTestCase.setFlowCfg(xml);
        autoUiTestCase.setFlowId(Long.valueOf(flowId));

        Integer modify = autoUiTestCaseService.modify(autoUiTestCase);
        return RespBean.ok("修改成功");
    }

    @PostMapping("queryCount")
    public Integer queryCount() {

        return autoUiTestCaseService.queryCount();
    }

    @PostMapping("queryAutoUiTestCaseByPage")
    public List<AutoUiTestCase> queryAutoUiTestCaseByPage(@RequestBody Map map) {
        int start = (int) map.get("start");
        int limit = (int) map.get("limit");
        return autoUiTestCaseService.queryByPage(start, limit);
    }

    @PostMapping("queryCfgByFlowId")
    public String queryCfgByFlowId(@RequestBody Map map) {
        String flowId = (String) map.get("flowId");

        return autoUiTestCaseService.queryCfgByFlowId(Integer.valueOf(flowId));
    }
}
